﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.IO;
using System.Reflection;


namespace NScheduler
{
    /// <summary>
    /// 配置项定义类
    /// </summary>
    internal class Config
    {
        static Config()
        {
            _taskTypes = new List<Type>(); //任务类型列表
        }

        #region 私有变量
        private static List<Type> _taskTypes;
        #endregion

        /// <summary>
        /// 获取任务的类型列表 
        /// &lt;section name="Import" type="NScheduler.Tasks.ImportTask, NScheduler.Tasks"/&gt;
        /// </summary>
        public static List<Type> TaskTypes
        {
            get
            {
                if (_taskTypes.Count > 0)
                    return _taskTypes;

                Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);
                var tasks = config.GetSectionGroup("NScheduler").Sections;
                foreach (ConfigurationSection item in tasks)
                {
                    var typeName = item.SectionInformation.Type;
                    var type = Type.GetType(typeName);
                    _taskTypes.Add(type);
                }

                return _taskTypes;
            }
        }

        /// <summary>
        /// 获到计划任务的队列类型 
        /// &lt;sectionGroup name="Scheduler" type="NScheduler.QueueScheduler,NScheduler.Core"&gt;
        /// </summary>
        public static Type SchedulerType
        {
            get
            {
                Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);
                var typeName = config.GetSectionGroup("NScheduler").Type;
                var type = Type.GetType(typeName);

                return type;
            }
        }

        /// <summary>
        /// 最大并行任务数
        /// </summary>
        public static int MaxTask
        {
            get
            {
                var result = ConfigurationManager.AppSettings.Get("MaxTask");
                if (string.IsNullOrWhiteSpace(result)) //为空时取默认值
                {
                    return 1;
                }

                return Convert.ToInt32(result);
            }
        }

        /// <summary>
        /// 最大并行任务数
        /// </summary>
        public static string ErrorLogger
        {
            get
            {
                var result = ConfigurationManager.AppSettings.Get("ErrorLogger");
                if (string.IsNullOrWhiteSpace(result)) //为空时取默认值
                {
                    return string.Empty;
                }

                return result;
            }
        }
    }
}
